/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_picture_list.js                                 
*  Description  : 개별클럽 사진게시판 메인
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-15  이윤노    Created.  
*  2012-05-15  이윤노    Complete.  
*******************************************************************************/


jQuery(function($){
	
	
	//상단메뉴
	function clsTopMenuInfo(){
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsTopMenuInfo.prototype = {
		_init: function(){
			var $layer = $("div#container nav ul.self_club_gnb");			
			this._HTMLDom = {
				"$layer":$layer
			}			
		}
		, getBtnClubHome: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:first");
		}
		, getBtnClubField: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(1)");
		}
		, getBtnClubBoard: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(2)");
		}
		, getBtnClubPic: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(3)");
		}
		, getBtnClubAdmin: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(4)");
		}
	}
	
	//전체게시판/내가쓴글 
	function clsClubBoardTitle(){
		this._HTMLDom = null;
		this._init();		
	}
	
	clsClubBoardTitle.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.depth2_tab");
			
			this._HTMLDom = {
				"$layer": $layer
				,"$tabs": $layer.find("li")
			};
			
			$layer.data("idx", null); // 인덱스로 관리
			this.display();
		}
		, $getsBtnTab: function(){
			var $layer = this._HTMLDom["$layer"];
			var $tabs = this._HTMLDom["$tabs"];
			
			return $tabs.find("a");
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			var idx = $layer.data("idx");
			
			// 현재 인덱싱된 타이틀만 on설정
			$tabs.find("img").each(function(n){
				var imgSrc = $(this).attr("src");
				var szImgName = imgSrc.replace(/(.*)\/(.*).gif/g, function(){
					return arguments[1] + "/" + arguments[2].replace("_on", "");
				});
				
				$(this).attr("src", szImgName + ((idx == n) ? "_on":"") + ".gif");
			});
		}
		, setIdx: function(val){
			var $layer = this._HTMLDom["$layer"];
			
			if(val == $layer.data("idx")) return false;
			
			$layer.data("idx", val);
			
			this.display();
			return true;
		}
			
	}
	
	//삭제
	function clsDeleteList(){
		this._HTMLDom = null
		this._ERRMSG = {
			"_NOVALUE": "선택한 항목이 없습니다."	
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsDeleteList.prototype = {
		_init: function(){
			var $layer = $("div.golf_board  article.btn_area");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getBtnDelete: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("a");
		}
		, isValid: function(){
			this.errMsg = null;
			
			if(this.getValue() == ""){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
	}
	
	//게시판글목록
	function clsClubList(){
		this._HTMLDom = null;
		this.listData = null;
		this.clubInfoData = null;
		
		this._init();		
	}
	
	clsClubList.prototype = {
		_init: function(){
			var $layer = $("div#container article.pic_list tbody");
			var $notice = $("div#container section#main_content div.golf_board dl dd");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
				, "$notice": $notice
				};
			$layer.empty();
			
		}
		, setData: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var $notice = this._HTMLDom["$notice"];
			var thisClass = this;
			
			$layer.empty();
			
			for(var i=0;i<this.listData.length/5;++i){
				$layer.append($element.clone(true));
			}
	
			$notice.html(this.clubInfoData["공지사항"]);
			
			$layer.find("td").each(function(n){
				if(thisClass.clubInfoData["내상태"] != "001") {
					$(this).find("p:eq(0)").data("s_key",thisClass.listData[n]["글번호"]).find("input:checkbox").prop("checked",false).hide().find("span").html(thisClass.listData[n]["글번호"]);
				}else {
					$(this).find("p:eq(0)").data("s_key",thisClass.listData[n]["글번호"]).find("input:checkbox").prop("checked",false).find("span").html(thisClass.listData[n]["글번호"]);
				}
				$(this).find("img").attr("src",thisClass.listData[n]["작성자"]["사진"]);
				$(this).find("a").click(function(event){
					event.preventDefault();
					
					document.location.href = "club_picture_view.html?q_my="+(document.objUtil.getParam("q_my"))+"&q_seq="+(thisClass.listData[n]["클럽키"]);
				});
				
				$(this).find("a em").html(thisClass.listData[n]["제목"]).append("<span>(" + thisClass.listData[n]["조회수"] + ")</span>");
				$(this).find("p.date").html(thisClass.listData[n]["날짜"]);
				$(this).find("p.name").html(thisClass.listData[n]["작성자"]["작성자"]);
				
			});

			//타이틀 체크박스
			if(thisClass.clubInfoData["내상태"] != "001") $layer.prev().find("th:first").html("");
			//삭제버튼
			if(thisClass.clubInfoData["내상태"] != "001") $layer.parents().find("div.golf_board  article.btn_area").hide();
			
		}
		, setClubInfo: function(clubData){			
			this.clubInfoData = clubData;
			
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent().hide();
			if($layer.parent().prev("div#tmpSpin").length == 0){ 
				$layer.parent().before("<div id='tmpSpin' style='position:relative; width:670px; height:500px;'></div>");
			}
			$layer.parent().prev("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent().prev("div#tmpSpin").spin(false).hide();
			$layer.parent().show();
		}
		//클릭 데이터 검색
		, getCheckData: function(){
						
			var checkData = "";
			
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("tr").each(function(n){
				if($(this).find("td:eq(0) input").prop("checked")){
					checkData += (checkData==""?"":",");
					checkData += $(this).find("td:eq(0) input").data("s_key");
				}
			});
			
			return checkData;
		}
		
	}
	
	// 검색타입
	function clsSearchType()
	{
		this._HTMLDom = null;
		this.idx = null;
		
		this._init();
	}
	
	clsSearchType.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search div.selectlayer");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			this.idx = 0;
			
			this._bind();
		}
		, _bind: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0) a").click(function(event){
				event.preventDefault();
				
				$layer.find("ul").show(300);
			});
			
			$layer.find("li a").click(function(event){
				event.preventDefault();
				
				thisClass.setIdx($layer.find("li a").index($(this)));
			});
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:eq(0) a").html($layer.find("li a").eq(this.idx).html());
		}
		, setIdx: function(idx){
			this.idx = idx;
			this.display();
			this._HTMLDom["$layer"].find("ul").hide(300);
		}
		, $getSelect: function(){
			return this._HTMLDom["$layer"].find("ul");
		}
		, getValue: function(){
			return this.idx;
		}
	}
	
	// 검색어
	function clsSearchText()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "검색어를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsSearchText.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.golf_board_search :text");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
		, isValid: function(){
			this.errMsg = null;
			
			if(this.getValue() == ""){
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
	}
		
	//AJAX 통신
	function clsComm()
	{
		this.clubData = null;
		this.clsClubBoardList = null;
		this.clsSearchText = null;
		this.clubBoardRequest = null;
		this._init();
	}
	
	clsComm.prototype = {
		
		_init: function(){
			this.clubKey = document.objUtil.getParam("q_seq");
			
			this.clubData = null;
			this.clsClubBoardList = null;
			this.clsClubBoardPageData = null;
			this.clsSearchText = null;
			this.clubBoardRequest = null;
			
			this.clubData = {
				"동호회명": null
				, "가입자수": null
				, "라운딩키": null
				, "라운딩제목": null
				, "가입여부": null
				, "가입유형": null
				, "내상태": null
				, "공지사항": null
			}
			
			this.clubBoardRequest = {
				"검색종류": null
				, "검색어": null
				, "페이지": null
				, "전체글/내글": null
			}
			
		}
		, getClubInfo: function(){
			var thisClass = this;
			var rnd = document.objUtil.random(0, 1);
			var rnd1 = document.objUtil.random(0, 1);
		
			//AJAX - 사용자 정보조회
			setTimeout(function(){
				thisClass.clubData = {
					"동호회명": "좋은 만남 훌륭한 동반자 - 헝그리 골프클럽"
					, "가입자수": "3694"
					, "라운딩키": "001"
					, "라운딩제목": "(진행) 신규회원분들을 위한 초보골퍼들을 위한 이벤트 라운딩"
					, "가입여부": rnd
					, "가입유형": rnd1
					, "내상태": "001"
					, "공지사항": "헝그리골프클럽 게시판 내 삭제 기준 및 유의사항입니다."
					
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			}, 100);
		}
		, getClubInfoData: function(){
			return this.clubData;
		}
		, getClubBoardPageData: function(){
			return this.clsClubBoardPageData;
		}
		, getClubBoardList: function(){
			var thisClass = this;
		
			//AJAX - 동호회 글목록 조회
			setTimeout(function(){ 
				thisClass.clsClubBoardPageData = {
					"전체수": 11
				}
				
				thisClass.clsClubBoardList  = [
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "10"
						, "제목": "비오는데 골프장에서"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "9"
						, "제목": "뭐하니 청승맞게"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "8"
						, "제목": "감기걸릴라"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱짱말짱짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "7"
						, "제목": "얼른 몸좀 녹이거라"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "6"
						, "제목": "제목이 노출됩니다."
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "5"
						, "제목": "제목이 노출됩니다~같이!!"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "4"
						, "제목": "제목이 노출됩니다~악쵸!!"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "3"
						, "제목": "제목이 노출됩니다~나와!!"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "2"
						, "제목": "제목이 노출됩니다~다다"
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					},
					{
						"삭제권한": "Y"
						, "클럽키": "1"
						, "글번호": "1"
						, "제목": "제목이 노출됩니다."
						, "작성자": {
													"사진": "../images/board/default_154_115.gif"
												, "작성자": "오스케 짱말짱"
												}
						, "날짜": "2012-03-03"
						, "조회수": "100"
					}];
				$(thisClass).trigger("evtFinishClubBoardList");
			}, 1000);
		}
		, getClubListData: function(){
			return this.clsClubBoardList;
		}
		, setClubBoardRequest: function(key, val){
			this.clubBoardRequest[key] = val;
		}
		, deleteClubBoardList: function(key){			
			var thisClass = this;
			//AJAX - 동호회 글목록 조회
			
			setTimeout(function(){
				var ret = {
					"결과코드": "001"
					, "메시지": null
				}
				
				$(thisClass).trigger("evtFinishClubDelete", [ret]);
			}, 500);
		}
	}
		
	//이벤트관리
	function clsControl()
	{
		this.objComm = null;
		
		this.objJoinroomPage = null;
		this.objClubList = null;
		this.objClubBoardTitle = new clsClubBoardTitle();
		
		this.objTopMenuInfo = null;
		this.objSearchType = null;
		this.objSearchText = null;
		this.objDeleteList = null;
		
		this.pageData = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();			
			//글목록,페이징
			this.objClubList = new clsClubList();
			
			this.objClubBoardPage = new document.objGlobal.clsPaging($("#idClubListPage"), 10, 10);
			
			//사용자정보
			this.objTopMenuInfo = new clsTopMenuInfo();			
			//공지사항
			this.objTopMenuInfo = new clsTopMenuInfo();			
			//전체글(default), 내가쓴글 관리
			this.objClubBoardTitle = new clsClubBoardTitle();
			
			//검색조건
			this.objSearchType = new clsSearchType();
			this.objSearchText = new clsSearchText();
			
			//삭제버튼
			this.objDeleteList = new clsDeleteList();
			
			this._bind();
			//전체글(0),내가쓴글(1) 인덱스설정 및 검색조건 
			this.objClubBoardTitle.setIdx((document.objUtil.getParam("q_my") == "Y") ? 1:0);
			//검색조건 설정(전체글 / 내가쓴글 )
			if(document.objUtil.getParam("q_my") == "Y") this.objComm.setClubBoardRequest("전체글/내글", true);
			
			this.objClubBoardPage.setPage(1);
			
			//동호회 정보
			this.objComm.getClubInfo();
			
		}
		, _bind: function(){
			var thisClass = this;
			
			//<--navi 페이지 설정 시작
			//홈
			this.objTopMenuInfo.getBtnClubHome().click(function(event){
				event.preventDefault();						
				document.location.href = "home.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//동호회필드
			this.objTopMenuInfo.getBtnClubField().click(function(event){
				event.preventDefault();						
				document.location.href = "club_field.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//일반게시판
			this.objTopMenuInfo.getBtnClubBoard().click(function(event){
				event.preventDefault();						
				document.location.href = "club_list.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//사진게시판
			this.objTopMenuInfo.getBtnClubPic().click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_list.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//관리하기
			this.objTopMenuInfo.getBtnClubAdmin().click(function(event){
				event.preventDefault();						
				document.location.href = "club_admin_info.html?q_seq=" + (document.objUtil.getParam("q_seq"));				
			});
			//navi 페이지 설정 시작 -->
			
			//글쓰기 이동
			$("div#container section#main_content article.depth2_tab p.btn_depth a").click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_write.html?q_my=" + (document.objUtil.getParam("q_my"));				
			});
				
			// 전체게시글, 내가쓴글
			this.objClubBoardTitle.$getsBtnTab().click(function(event){
				event.preventDefault();
				var idx = thisClass.objClubBoardTitle.$getsBtnTab().index($(this));
				
				document.location.href = document.location.pathname + ((idx) ? "?q_my=Y":"");
			});
			
			//동호회 사용자 조회 - AJAX 완료 이벤트 
			$(this.objComm).bind("evtFinishClubInfo", function(){
				thisClass.objClubList.spinStart();
				thisClass.objComm.getClubBoardList();
			});
			
			//동호회 글목록 조회 - AJAX 완료 이벤트 
			$(this.objComm).bind("evtFinishClubBoardList", function(){
				var thisData = thisClass.objComm.getClubListData()
				var pageData = thisClass.objComm.getClubBoardPageData();
				var clubInfoData = thisClass.objComm.getClubInfoData();
				
				thisClass.objClubList.setClubInfo(clubInfoData);
				thisClass.objClubList.spinEnd();
				thisClass.objClubList.setData(thisData);
				
				// 페이지 부에 전체 수설정 
				thisClass.objClubBoardPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubBoardPage.display();
				
			});
			
			// 페이지이동 이벤트
			$(this.objClubBoardPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setClubBoardRequest("페이지", thisPage);
				thisClass.objClubList.spinStart();
				thisClass.objComm.getClubBoardList();
			});
			
			// 삭제완료 이벤트
			$(this.objComm).bind("evtFinishClubDelete", function(){
				var thisPage = thisClass.objClubBoardPage.getPage();
				
				alert("삭제 완료되었습니다.");
				
				thisClass.objComm.setClubBoardRequest("페이지", thisPage);
				thisClass.objClubList.spinStart();
				thisClass.objComm.getClubBoardList();
			});
			
			// 전체게시글, 내가쓴글
			this.objClubBoardTitle.$getsBtnTab().click(function(event){
				event.preventDefault();
				var idx = thisClass.objClubBoardTitle.$getsBtnTab().index($(this));
				
				document.location.href = document.location.pathname + ((idx) ? "?q_my=Y":"");
			});
			
			// 검색 이벤트
			$("article.golf_board_search :image").click(function(event){
				event.preventDefault();
				
				if(!thisClass.objSearchText.isValid()){
					alert(thisClass.objSearchText.getError());
					thisClass.objSearchText.focus();
					return;
				}
				
				thisClass.objComm.setClubBoardRequest("검색종류", thisClass.objSearchType.getValue());
				thisClass.objComm.setClubBoardRequest("검색어", thisClass.objSearchText.getValue());
				thisClass.objClubBoardPage.setPage(1);
				thisClass.objClubList.spinStart();
				thisClass.objComm.getClubBoardList();
			});
			
			// 삭제버튼 이벤트
			this.objDeleteList.$getBtnDelete().click(function(event){
				event.preventDefault();
				
				var clubData = thisClass.objComm.clubData;
				var checkData = thisClass.objClubList.getCheckData();
				
				//권한체크
				if(thisClass.objComm.clubData["내상태"] != "001") {
					alert('권한불충분');
					return;
				}
				
				thisClass.objComm.deleteClubBoardList(checkData);
			});
			
			
			
		}
		
		
	}
	
	document.objControl = new clsControl();
});